using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PrinterHack
{
    public partial class frmPrinters : Form
    {
        private Form parentForm;
        public frmPrinters()
        {
            InitializeComponent();
        }

        public frmPrinters(Form ParentForm)
        {
            InitializeComponent();
            parentForm = ParentForm;
        }

        private void frmPrinters_Load(object sender, EventArgs e)
        {
            cboPrinters.DataSource = Printer.PrinterData.Tables["Printer"];
            cboPrinters.DisplayMember = "Name";
            cboPrinters.ValueMember = "ID";

            if (cboPrinters.Text != "")
            {
                Printer printer = new Printer();
                printer.LoadDataByName(cboPrinters.Text);
                txtName.Text = printer.Name;
                txtIP.Text = printer.IP;
                txtDescription.Text = printer.Description;
            }
        }

        private void cboPrinters_SelectedIndexChanged(object sender, EventArgs e)
        {
            Printer printer = new Printer(cboPrinters.SelectedValue.ToString());
            txtName.Text = printer.Name;
            txtIP.Text = printer.IP;
            txtDescription.Text = printer.Description;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Printer printer = new Printer(cboPrinters.SelectedValue.ToString());
                printer.Name = txtName.Text;
                printer.IP = txtIP.Text;
                printer.Description = txtDescription.Text;
                printer.Save();
                MessageBox.Show("Printer settings updated.", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);

                cboPrinters.DataSource = Printer.PrinterData.Tables["Printer"];
                cboPrinters.DisplayMember = "Name";
                cboPrinters.ValueMember = "ID";

                frmMain parent = (frmMain)parentForm;
                parent.RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboPrinters_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                cboPrinters_SelectedIndexChanged(sender, e);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}